package com.xh.bussiness.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.course.bean.request.CoursewareRequest;
import com.xh.bussiness.course.dao.CoursewareMapper;
import com.xh.bussiness.course.model.Courseware;
import com.xh.bussiness.course.service.ICoursewareService;
import com.xh.core.bean.XhPage;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.core.util.DateUtils;
import com.xh.core.util.UserUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
public class CoursewareSerivceImpl extends BaseServiceImpl<CoursewareMapper, Courseware> implements ICoursewareService {

    @Autowired
    private CoursewareMapper coursewareMapper;

    @Override
    public XhPage queryPage(XhPage page, CoursewareRequest bean) {
        bean.setCreatorId(UserUtils.getCurrentUserId());
        return coursewareMapper.queryByPage(page,bean);
    }

    @Override
    public String saveCourseware(CoursewareRequest bean) {
        Courseware courseware =new Courseware();
        BeanUtils.copyProperties(bean,courseware);
        this.dao.insert(courseware);

        return courseware.getId();
    }

    @Override
    public boolean modify(CoursewareRequest bean) {
        Courseware courseware = this.dao.selectById(bean.getId());
        if(null == courseware){
            return false;
        }
        if(null != bean.getAuditStatus()){
            courseware.setAuditStatus(bean.getAuditStatus());
        }
        if(null != bean.getSubmitTime()){
            courseware.setSubmitTime(DateUtils.parse(bean.getSubmitTime(), "yyyy-MM-dd HH:mm:ss"));
        }
        return this.dao.updateById(courseware) > 0;



    }


    /**
     * 准备条件
     *
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(CoursewareRequest bean, QueryWrapper qw) {
        if (bean.getIds() != null && bean.getIds().length > 0) {
            qw.in("id", bean.getIds());
        }
        return qw;
    }
}
